package logobj.dataAnalysis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import logobj.config.ConfProperties;
import logobj.dao.LogDao;
import logobj.entity.analysis.XnqcJbxxVo;
import logobj.utils.TimeUtils;
import logobj.utils.db.DBCon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Scanner;

public class AnalysisNew {

    public void inputData(String logpath){
        File dir = new File(logpath);
        if(dir.exists() && dir.isDirectory()){
            File[] filelist = dir.listFiles();
            for (File f : filelist){
                if(f.isFile()){
                    analydata(f.getAbsolutePath());
                }
            }
        }
    }


    public void analydata(String filename){
        FileInputStream inputStream = null;
        Scanner sc = null;
        String line = null;
        try{
            inputStream = new FileInputStream(filename);
            sc = new Scanner(inputStream, "UTF-8");
            ArrayList<XnqcJbxxVo> xnqcJbxxVoArrayList = new ArrayList<>();
            int i = 0;
            LogDao logDao = new LogDao();
            while(sc.hasNextLine()) {
                line = sc.nextLine();
                //去掉日志前面的多余字符，保留json字符串
                if(line.indexOf("{") > -1){
                    line =  line.substring(line.indexOf("{"));
                }
                xnqcJbxxVoArrayList.add(analysisStrToObject(line));
                if(i %ConfProperties.data_warehousing_shz_maxrow == 0 && i !=0){
                    logDao.insertXnqcData(xnqcJbxxVoArrayList);
                    xnqcJbxxVoArrayList.clear();
                }
            }
            if(xnqcJbxxVoArrayList.size() > 0){
                logDao.insertXnqcData(xnqcJbxxVoArrayList);
                xnqcJbxxVoArrayList.clear();
            }
            if(sc.ioException() != null) {
                throw sc.ioException();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sc != null) {
                sc.close();
            }

        }
        System.gc();

    }

    /**
     * json字符串解析
     * @param jsonstr json格式的字符串
     * @return void
     * */
    public XnqcJbxxVo analysisStrToObject(String jsonstr){

        JSONObject jsonobj = JSON.parseObject(jsonstr); //将json字符串转换成jsonObject对象
        //字符串转换json对象
        JSONObject jsonInputData = jsonobj.getJSONObject("InputData");
        JSONObject jsonCheckResult = jsonobj.getJSONObject("CheckResult");

        JSONObject net = jsonInputData.getJSONObject("net");

        JSONObject jsonAssets = jsonInputData.getJSONObject("assets");
        JSONObject jsonIoc = jsonCheckResult.getJSONObject("ioc");
        JSONObject mc_geo_data = jsonInputData.getJSONObject("mc_geo_data");

        String machine = jsonInputData.getString("machine");
        String src_ip =  net == null ? null : net.getString("src_ip") ;
        String basic_tag =  jsonIoc == null ? null : jsonIoc.getString("basic_tag") ;
        String assets_name =  jsonAssets == null ? null : jsonAssets.getString("name") ;
        String severity =  jsonIoc == null ? null : jsonIoc.getString("severity") ;
        String lat =   mc_geo_data == null ? null : mc_geo_data.getString("Latitude") ;
        String lng =   mc_geo_data == null ? null : mc_geo_data.getString("Longitude") ;
        Timestamp gjsj = jsonInputData.getTimestamp("@timestamp");
        String rkrq  = TimeUtils.getStringNowDate("yyyy-MM-dd") ;

        //获取json数据
        return new XnqcJbxxVo(machine,src_ip,basic_tag,assets_name,severity,lat,lng,gjsj,rkrq);
    }







}
